package jetpack.com.pzl.works;

import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.work.Worker;
import androidx.work.WorkerParameters;

/**
 * 一个模拟下载的任务类
 */
public class DownloadWorker extends Worker {
    final String TAG = "jinx_DownloadWorker";

    public DownloadWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
        super(context, workerParams);
    }

    @NonNull
    @Override
    public Result doWork() {
        Log.i(TAG, "DownloadWorker doWork : " + Thread.currentThread().getName());
        if (!downloadFiles()) {
            Log.i(TAG, "DownloadWorker doWork : " + Thread.currentThread().getName() + " 下载失败");
            return Result.retry();
        } else {
            Log.i(TAG, "DownloadWorker doWork : " + Thread.currentThread().getName() + " 下载成功");
            return Result.success();
        }
    }

    /**
     * 模拟下载文件
     */
    private boolean downloadFiles() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //下载失败
        return false;
    }

}
